package spider;

import com.sun.org.apache.xpath.internal.SourceTree;
import entity.PoemInfo;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author huwei
 * 将全部的HTML进行初筛后的信息存入信息对象, 并返回它
 */
public class PullFromWeb {
	public static List<PoemInfo> pull(String url) {
		List<PoemInfo> poemInfoList = new ArrayList<PoemInfo>();
		try {
			Document document = Jsoup.connect(url).timeout(999999999).get();
			// 去掉右边的部分，因为它的className 也叫sons
			Elements fakesSons = document.getElementsByClass("right");
			fakesSons.remove();
			// 获取诗词所在的父元素
			Elements sons = document.getElementsByClass("sons");
			for(Element son:sons){
				Element cont = son.child(0);
				 // 获取诗名
				Element p_title = cont.child(1);
				 // 获取作者
				Element p_source = cont.child(2);
				 // 获取内容
				Element p_contson = cont.child(3);
				// 获取标签
				Elements tag = son.getElementsByClass("tag");
				if (tag==null||tag.isEmpty()||tag.size()>1) {
					continue;
				}
				PoemInfo p = new PoemInfo();
				p.title = p_title.text();
				p.content = p_contson.text();
				p.author = p_source.text();
				p.tag = tag.get(0).text();

				poemInfoList.add(p);
			}
			return poemInfoList;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
